﻿using B2B.Bussiness;
using B2B.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace B2B.Views
{
    public partial class SiparisListesi : Page
    {
        DataServiceClient client = new DataServiceClient();
        Kullanici usr = Session.Instance.GetValue<Kullanici>("USER");

        public SiparisListesi()
        {
            InitializeComponent();
            this.Loaded += SiparisListesi_Loaded;
            btnAra.Click += btnAra_Click;
            client.SiparisListeleCompleted += client_SiparisListeleCompleted;
            client.SiparisBulCompleted += client_SiparisBulCompleted;
            client.SiparisSilCompleted += client_SiparisSilCompleted;
            btnSecimiSil.Click += btnSecimiSil_Click;
            btnSecilenleriOnayla.Click += btnSecilenleriOnayla_Click;
            client.SiparisOnaylaCompleted += client_SiparisOnaylaCompleted;
        }

        void client_SiparisOnaylaCompleted(object sender, SiparisOnaylaCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Sipariş Onaylandı !");
            }
        }

        void btnSecilenleriOnayla_Click(object sender, RoutedEventArgs e)
        {
            if (!usr.OnayVerebilir)
            {
                MessageBox.Show("Sipariş onay vermeye yetkiniz yok !");
                return;
            }
            ObservableCollection<SiparisOzet> lst = (ObservableCollection<SiparisOzet>)grdListe.ItemsSource;
            foreach (SiparisOzet item in lst)
            {
                if (item.Sec)
                {
                    client.SiparisOnaylaAsync(item.Seri, item.Sira, usr.ID);
                }
            }
        }

        void btnSecimiSil_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<SiparisOzet> lst = (ObservableCollection<SiparisOzet>)grdListe.ItemsSource;
            foreach (SiparisOzet item in lst)
            {
                if (item.Sec)
                {
                    client.SiparisSilAsync(item.Seri, item.Sira);
                }
            }
        }

        void client_SiparisSilCompleted(object sender, SiparisSilCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Sipariş Silindi !");
            }
        }

        void SiparisListesi_Loaded(object sender, RoutedEventArgs e)
        {
            dteBaslangic.SelectedDate = new DateTime(DateTime.Now.Year, 1, 1);
            dteBitis.SelectedDate = new DateTime(DateTime.Now.Year, 12, 31);
            cmbDurum.SelectedIndex = 2;
        }

        void client_SiparisBulCompleted(object sender, SiparisBulCompletedEventArgs e)
        {
            SiparisSonuc frm = new SiparisSonuc();
            List<Order> lst = new List<Order>();
            foreach (SiparisDetay s in e.Result)
            {
                Order o = new Order();
                o.Adi = s.Adi;
                o.Fiyat = s.Fiyat;
                o.Iskonto1 = s.Iskonto;
                o.KdvOrani = s.KdvOrani;
                o.Kodu = s.Kod;
                o.Miktar = s.Adet;
                o.Sec = s.Sec;
                o.Yekun = s.Yekun;
                o.KdvTutari = s.KdvTutari;
                lst.Add(o);
            }
            frm.SiparisListe = lst;
            frm.Goruntule = true;
            bsyIndic.IsBusy = false;
            frm.Show();
        }

        void client_SiparisListeleCompleted(object sender, SiparisListeleCompletedEventArgs e)
        {
            grdListe.ItemsSource = e.Result;
            bsyIndic.IsBusy = false;
        }

        void btnAra_Click(object sender, RoutedEventArgs e)
        {
            Cari cr = Session.Instance.GetValue<Cari>("FIRMA");
            if (cr != null)
            {
                if (!dteBaslangic.SelectedDate.HasValue)
                    dteBaslangic.SelectedDate = new DateTime(DateTime.Now.Year, 1, 1);

                if (!dteBitis.SelectedDate.HasValue)
                    dteBitis.SelectedDate = new DateTime(DateTime.Now.Year, 12, 31);

                bsyIndic.IsBusy = true;
                client.SiparisListeleAsync(cr.ID, Convert.ToByte(cmbDurum.SelectedIndex), dteBaslangic.SelectedDate.Value, dteBitis.SelectedDate.Value);
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void SiparisDetayi(object sender, RoutedEventArgs e)
        {
            var ctl = e.OriginalSource as Button;
            if (null != ctl)
            {
                var sip = ctl.DataContext as SiparisOzet;
                if (null != sip)
                {
                    bsyIndic.IsBusy = true;
                    client.SiparisBulAsync(sip.Seri, sip.Sira);
                }
            }
        }
    }
}
